//
//  GoodStandardSelectCell.m
//  B2C
//
//  Created by xiaowu on 2017/8/2.
//  Copyright © 2017年 lixiaolong. All rights reserved.
//

#import "GoodStandardSelectCell.h"

static CGFloat commonOffset = 15;
static CGFloat itemOffset = 10;
static CGFloat titleLabelHeight = 44;
static CGFloat buttonHeight = 32;
static CGFloat baseTag = 412309;

@interface GoodStandardSelectCell ()
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UIColor *selectColor;
@property (nonatomic, strong) NSArray *arr;
@end

@implementation GoodStandardSelectCell

+ (CGFloat)getHeightWithArr:(NSArray *)arr {
    CGFloat height = 0 ;
    
    
    
    CGFloat w = commonOffset;
    CGFloat number = 0;
    
    for (NSInteger i = 0; i < arr.count; i++) {
        NSString *str = [arr safeObjectAtIndex:i];
        
        //根据计算文字的大小
        NSDictionary *attributes = @{NSFontAttributeName:TextNormalFont14};
        CGFloat length = [str boundingRectWithSize:CGSizeMake(ScreenWidth, 2000) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.width;
        length = ceilf(length);
        // 先判断是否超出屏幕
        if (w + length + commonOffset * 2 + itemOffset * 2 > ScreenWidth) {
            number += 1;
            w = commonOffset;
        }
        w = w + itemOffset + (length + commonOffset * 2);
    }
    if (arr.count != 0) {
        height = (number + 1) * (commonOffset + buttonHeight);
    }
    
    height += titleLabelHeight;
    return height;
}


#pragma Mark - Init
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        _currentIndex = -1;
        _selectColor = [UIColor orangePriceColor];
        _arr = [NSArray array];
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.titleLabel.frame = CGRectMake(15, 0, self.contentView.width - 30, titleLabelHeight);
    self.lineView.frame = CGRectMake(15, self.height - 0.5, self.contentView.width - 30, 0.5);
}

- (void)setTitlesArr:(NSArray *)arr withCanSelectArr:(NSArray *)canSelectArr withShowNormal:(BOOL)show withTitle:(NSString *)title withColor:(UIColor *)color withSelectIndex:(NSInteger)index {
    [self.contentView removeAllSubviews];
    
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.lineView];
    
    self.arr = arr;
    
    self.titleLabel.text = title;
    self.selectColor = color;
    
    CGFloat w = commonOffset;
    CGFloat number = 0;
    
    for (NSInteger i = 0; i < arr.count; i++) {
        GoodAttributeValueModel *model = [arr safeObjectAtIndex:i];
        NSString *str = model.taname;
        //根据计算文字的大小
        NSDictionary *attributes = @{NSFontAttributeName:TextNormalFont14};
        CGFloat length = [str boundingRectWithSize:CGSizeMake(ScreenWidth, 2000) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.width;
        length = ceilf(length);
        // 先判断是否超出屏幕
        if (w + length + commonOffset * 2 + itemOffset * 2 > ScreenWidth) {
            number += 1;
            w = commonOffset;
        }
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(w, titleLabelHeight + number * (commonOffset + buttonHeight), (length + commonOffset * 2), buttonHeight);
        button.tag = i + baseTag;
        [button setTitle:str forState:UIControlStateNormal];
        [button setTitleColor:[UIColor grayTextColor] forState:UIControlStateNormal];
        button.titleLabel.font = TextNormalFont14;
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [button setBackgroundColor:[UIColor grayF9Color]];
        [button setBackgroundImage:[UIImage imageWithColor:[UIColor blackColor]] forState:UIControlStateDisabled];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateDisabled];
        [self.contentView addSubview:button];
        
        w = w + itemOffset + length + commonOffset * 2;
        
        button.enabled = NO;
        for (NSInteger j = 0; j < canSelectArr.count; j++) {
            PriceTaidModel *taidModel = [canSelectArr safeObjectAtIndex:j];
            if ([model.taid isEqualToString:taidModel.taid]) {
                button.enabled = YES;
            }
        }
        if (show) {
            button.enabled = YES;
        }
//        if (index != -1) {
//            button.enabled = YES;
//        }
        if (i == index) {
            [button setBackgroundImage:[UIImage imageWithColor:self.selectColor] forState:UIControlStateNormal];
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            button.enabled = YES;
            self.currentIndex = index;
        }
    }
    
}

- (void)setTitlesArr:(NSArray *)arr withTitle:(NSString *)title withColor:(UIColor *)color {
    [self.contentView removeAllSubviews];
    
    self.currentIndex = -1;
    
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.lineView];
    
    self.arr = arr;
    
    self.titleLabel.text = title;
    self.selectColor = color;
    
    CGFloat w = commonOffset;
    CGFloat number = 0;
    
    for (NSInteger i = 0; i < arr.count; i++) {
        GoodAttributeValueModel *model = [arr safeObjectAtIndex:i];
        NSString *str = model.taname;
        //根据计算文字的大小
        NSDictionary *attributes = @{NSFontAttributeName:TextNormalFont14};
        CGFloat length = [str boundingRectWithSize:CGSizeMake(ScreenWidth, 2000) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.width;
        length = ceilf(length);
        // 先判断是否超出屏幕
        if (w + length + commonOffset * 2 + itemOffset * 2 > ScreenWidth) {
            number += 1;
            w = commonOffset;
        }
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(w, titleLabelHeight + number * (commonOffset + buttonHeight), (length + commonOffset * 2), buttonHeight);
        button.tag = i + baseTag;
        [button setTitle:str forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackTextColor] forState:UIControlStateNormal];
        button.titleLabel.font = TextNormalFont14;
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [button setBackgroundColor:[UIColor grayF5Color]];
        [button setBackgroundImage:[UIImage imageWithColor:[UIColor grayF5Color]] forState:UIControlStateDisabled];
        [button setTitleColor:[UIColor borderColor] forState:UIControlStateDisabled];
        [self.contentView addSubview:button];
        
        w = w + itemOffset + button.width;
        
        button.enabled = model.canSelect;
        if (model.isSelected) {
            [button setBackgroundImage:[UIImage imageWithColor:self.selectColor] forState:UIControlStateNormal];
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            button.enabled = YES;
            self.currentIndex = i;
        }
    }
}

- (void)changeButtonUI:(UIButton *)sender {
//    for (NSInteger i = 0; self.contentView.subviews.count; i++) {
//        UIView *view = [self.contentView.subviews safeObjectAtIndex:i];
//        
//        if ([view isKindOfClass:[UIButton class]]) {
//            UIButton *button = (UIButton *)view;
//            GoodAttributeValueModel *model = [self.arr safeObjectAtIndex:view.tag - baseTag];
//            [button setTitleColor:[UIColor blackTextColor] forState:UIControlStateNormal];
//            [button setBackgroundImage:[UIImage imageWithColor:[UIColor grayF9Color]] forState:UIControlStateNormal];
//            button.enabled = model.canSelect;
//        }
//    }
    
    for (UIView *view in self.contentView.subviews) {
        if ([view isKindOfClass:[UIButton class]]) {
            UIButton *button = (UIButton *)view;
            [button setBackgroundImage:[UIImage imageWithColor:[UIColor grayF9Color]] forState:UIControlStateNormal];
            [button setTitleColor:[UIColor blackTextColor] forState:UIControlStateNormal];
            GoodAttributeValueModel *model = [self.arr safeObjectAtIndex:(view.tag - baseTag)];
            button.enabled = model.canSelect;
        }
    }
    
    
    if (self.currentIndex != sender.tag - baseTag) {
        self.currentIndex = sender.tag - baseTag;
        [sender setBackgroundImage:[UIImage imageWithColor:self.selectColor] forState:UIControlStateNormal];
        [sender setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    } else {
        self.currentIndex = -1;
    }
}

- (void)buttonClick:(UIButton *)sender {

    [self changeButtonUI:sender];
    if (self.block) {
        self.block(self.currentIndex);
    }
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

#pragma mark - Getters
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor = [UIColor blackTextColor];
        _titleLabel.font = TextNormalFont16;
    }
    return _titleLabel;
}

- (UIView *)lineView {
    if (!_lineView) {
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = [UIColor lightGrayColor];
    }
    return _lineView;
}
@end
